<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--任务管理数据-->
      <el-col>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" >

          <el-form-item label="计划完成时间" prop="planCompletionStartTime" label-width="120px">
            <el-date-picker
              v-model="planComplete"
              clearable
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="被监督单位" prop="supervisor" label-width="120px">
            <el-input
              v-model="queryParams.supervisor"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              size="mini"
              @click="handleAdd"
            >添加</el-button>
          </el-col>
          <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>

        <el-table v-loading="loading" :data="infoList">
          <el-table-column type="index" width="50" align="center" label="序号" />
          <el-table-column label="被监督单位" align="center" prop="enterpriseName" />
          <el-table-column label="检查专业" align="center" prop="checkProfessionName" width="180px"/>
          <el-table-column label="镇/街道" align="center" prop="town" />
          <el-table-column label="经营地址" align="center" prop="addr" />
          <el-table-column label="监督机构" align="center" prop="superviseOrg" />
          <el-table-column label="监督员" align="center" prop="supervisor" />
          <el-table-column label="计划完成时间" align="center" prop="planCompletionTime" width="150px"/>
          <el-table-column label="合格状态" align="center" prop="qualifiedStatus" width="150px"/>
          <el-table-column label="执行状态" align="center" prop="implementStatus" >
            <template slot-scope="scope">
             <span style="margin-left: 10px">{{
              scope.row.status == "1"
                ? "执行中"
                : scope.row.status == "2"
                ? "已完成"
                : "未执行"
            }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
            <span @click="handleInfo(scope.row,'DETAIL')" style="color:#5D85FF;cursor: pointer;">查看详情</span>
            <!-- <span @click="handleInfo(scope.row,'SUPERVISORY_OPINION')"  style="margin-left: 10px;color:#5D85FF;cursor: pointer;">监督意见书</span> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getCheckTaskFn"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog title="添加检查任务" :visible.sync="open" width="75%" append-to-body>
        <el-form :model="formData" ref="formData" size="small" :inline="true">
        <el-row>
          <el-col :span="10">
            <el-form-item label="计划完成时间" prop="planCompletionTime">
               <el-date-picker
                v-model="params.planCompletionTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="date"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item label="监管员">
              <el-select v-model="params.supervisorId" clearable placeholder="请选择监管员">
              <el-option
                v-for="item in supervisorList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId">
              </el-option>
            </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        </el-form>

        <el-table :data="inspect" :height="inspectHeight" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="名称" align="center" prop="enterpriseName" />
          <el-table-column label="统一信用代码/登记号" align="center"  prop="creditCode"/>
          <el-table-column label="检查任务" align="center" prop="checkProfessionName"/>
         <el-table-column label="类别" align="center" prop="categoryListName"/>
          <el-table-column label="所在镇/街道" align="center" prop="town" />
          <el-table-column label="详细地址" align="center" prop="addr"/>
          <el-table-column label="联系电话" align="center" prop="" />
          <el-table-column label="监督员" align="center" prop="supervisor" />
        </el-table>

        <pagination
          v-show="inspectTotal>0"
          :total="inspectTotal"
          :page.sync="formData.pageNum"
          :limit.sync="formData.pageSize"
          @pagination="getEmployerListFn"
        />

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
    import { getCheckTask,getEmployerList,getSaveInspectTask,getSupervisorList} from "@/api/zhangping/occupational";
  export default {
    data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          infoList: [],
          inspect:[],
          dispositionData:'',
          planComplete:'',
          // 查询参数
          queryParams: {  
              planCompletionStartTime:'', // 开始时间
              planCompletionEndTime:'',  // 结束时间
              supervisor:'', // 监管人员  
              pageNum: 1,
              pageSize: 10,
          },
          formData:{
               pageNum: 1,
               pageSize: 10,
          },
          params:{
              planCompletionTime:'',
              supervisorId:'',
              enterpriseIdList:[]
          },
          ids: [],
          inspectHeight:'500',
          inspectTotal:0,
          open:false,
          scaleOpen:false,  
          supervisorList:[]
      };
    },

    created() {
       this.loading = false
       this.getCheckTaskFn()
       this.getEmployerListFn()
       this.getSupervisorListFn()
    },
    methods: {
       getCheckTaskFn(){
          if(this.planComplete !=null && this.planComplete != undefined){
              if(this.planComplete.length > 0){
                    this.queryParams.planCompletionStartTime = this.planComplete[0]
                    this.queryParams.planCompletionEndTime = this.planComplete[1]
              }    
          }else{
               this.queryParams.planCompletionStartTime = ''
               this.queryParams.planCompletionEndTime = ''
          }       
         getCheckTask(this.queryParams).then(res=>{
                if(res.code == 200){
                     this.infoList = res.rows
                     this.total = res.total
                }else{
                     this.$message.error(res.msg);  
                }    
         })
    },
    getEmployerListFn(){
         getEmployerList(this.formData).then(res=>{
                if(res.code == 200){
                     this.inspect = res.rows
                     this.inspectTotal = res.total
                }else{
                     this.$message.error(res.msg);  
                }    
         })
    },
    getSaveInspectTaskFn(){
         getSaveInspectTask(this.params).then(res=>{
                if(res.code == 200){
                      this.$message.success(res.msg);
                      this.open = false
                      this.getCheckTaskFn()
                      this.reset()
                }else{
                      this.$message.error(res.msg);  
                }    
          })            
    },
    // 监管人员下拉
     getSupervisorListFn(){
         getSupervisorList().then(res=>{
                if(res.code == 200){
                     this.supervisorList = res.data
                }else{
                     this.$message.error(res.msg);  
                }    
         })
    },

      handleSelectionChange(selection) {
      this.params.enterpriseIdList = selection.map(item => item.id)
    },

    submitForm(){
        if(this.params.planCompletionTime == '' || this.params.planCompletionTime == null){  
                     return this.$message.error('请选择计划完成时间'); 
        }
        if(this.params.supervisorId == '' || this.params.supervisorId == null){
                     return this.$message.error('请选择监管员');  
         }
         if(this.params.enterpriseIdList == ''){
                     return this.$message.error('请添加检查任务');  
         }
         this.getSaveInspectTaskFn()
    },
    handleAdd(){
          this.open = true
     },
     handleInfo(item,type){
          if(type === 'DETAIL'){
             this.$router.push(`/careerHygiene/taskInfo/inspectDetail`)       
          }else{
              
          }      
     },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getCheckTaskFn()
    },
    reset() {
      this.queryParams = {
          planCompletionStartTime:'', // 开始时间
          planCompletionEndTime:'',  // 结束时间
          supervisor:'',   // 监督人员
          pageNum: 1,
          pageSize: 10,
      };
    },
    cancel(){
         this.open = false
    },
     resetQuery(){
        this.reset()
     }
    }
  };
</script>
